
//登陆过期，跳转登录页面
function toLogin() {
    layer.msg('请重新登录',{icon:5,time:1500},function(){
        localStorage.clear();
        window.location.href='../template/login.html';
        return false;
    });
}

//封装登录
function ajax_login(url,params,httpCallBack){
    $.ajax({
        type:'POST',
        url:url,
        data:params,
        dataType:"json",
        success:function(res){
            console.log(res);
            if(res.status == 0){
                layer.msg(res.msg,{icon:1,time:1000},function(){
                    localStorage.setItem('userInfo',JSON.stringify(res.data));
                    localStorage.setItem('hosp_id',res.data.hosp_id);
                    localStorage.setItem('dept_id',res.data.dept_id);
                    localStorage.setItem('doc_id',res.data.user_id);
                    localStorage.setItem('center_name',res.data.center_name);
                    localStorage.setItem('hosp_name',res.data.hosp_name);
                    localStorage.setItem('dept_name',res.data.dept_name);
                    localStorage.setItem('doc_id',res.data.user_id);
                    localStorage.setItem('token',res.data.access_token);
                    //window.location.href='../template/indexboard.html';
                });
            }else{
                layer.msg(res.msg,{icon:5,time:1500},function(){
                    return false;
                });
            }
            httpCallBack(res);
        }
    })
}

//token存储
function saveLocalStorage(name,val){
    return localStorage.setItem(name,val);
}

//token读取
function getLocalStorage(name){
    return localStorage.getItem(name);
}

//封装post
function ajax_post(url,params,httpCallBack){
    /*if(check_login() === false){
        toLogin();
    }*/
    var token = getLocalStorage('token');
    var headers = {};
    if (token) {
        headers.Authorization = 'Bearer ' + token;
    }
    $.ajax({
        type:'POST',
        url:url,
        data:params,
        dataType:"json",
        headers:headers,
        success:function(res){
            if(res.status == 403){
                toLogin();
            }
            httpCallBack(res)
        }
    })
}

//封装get
function ajax_get(url,params,httpCallBack){
    var token = getLocalStorage('token');
    var headers = {};
    if (token) {
        headers.Authorization = 'Bearer ' + token;
    }
    $.ajax({
        type:'GET',
        url:url,
        data:params,
        dataType:"json",
        headers:headers,
        success:function(res){
            if(res.status == 403){
                toLogin();
            }
            httpCallBack(res)
        }
    })
}

function my_ajax(url,params,type,callback) {
    var token = getLocalStorage('token');
    var headers = {};
    if (token) {
        headers.Authorization = 'Bearer ' + token;
    }
    $.ajax({
        type:type,
        url:url,
        data:params,
        dataType:"json",
        headers:headers,
        success:function(res){
            if(res.status == 403){
                toLogin();
            }
            callback(res)
        }
    })
}


function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}



//封装post2
function ajax_post2(url,params,httpCallBack){
    /*if(check_login() === false){
        toLogin();
    }*/
    var token = getLocalStorage('token');
    var headers = {};
    if (token) {
        headers.Authorization = 'Bearer ' + token;
    }
    $.ajax({
        type:'POST',
        url:url,
        data:JSON.stringify(params),
        contentType: "application/json;charset=utf-8",
        dataType:"json",
        headers:headers,
        success:function(res){
            if(res.status == 403){
                toLogin();
            }
            httpCallBack(res)
        }
    })
}

function getCompletion(){

    ajax_get(configUrl.getCompletion,{user_id:getLocalStorage('patient_id'),visit_level:1},(res)=>{
        saveLocalStorage('completion',JSON.stringify(res.data));
        //console.log(JSON.parse(getLocalStorage('completion')));
        console.log('complate:');
        console.log(res)
        //getCompletion();
        var completion = JSON.parse(getLocalStorage('completion'));
        $('.complete_basic').text(Math.round(completion.basic)+'%');
        $('.complete_qss').text(Math.round(completion.bscj)+'%');
        $('.complete_qss1').text(Math.round(completion.template_11)+'%');
        $('.complete_qss2').text(Math.round(completion.template_12)+'%');
        $('.complete_qss3').text(Math.round(completion.template_13)+'%');
        $('.complete_qss4').text(Math.round(completion.template_14)+'%');
        $('.complete_qss5').text(Math.round(completion.template_15)+'%');
        $('.complete_qss6').text(Math.round(completion.template_16)+'%');
    });
}
